package com.study.manage.mvc.common;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.manage.dal.BlogBehaviorRepository;
import com.study.manage.dal.BlogMediaRepository;
import com.study.manage.dal.BlogRepository;
import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateApplyWorkRepository;
import com.study.manage.dal.EvaluateRepository;
import com.study.manage.dal.OrderRepository;
import com.study.manage.domain.Blog;
import com.study.manage.domain.BlogBehavior;
import com.study.manage.domain.Evaluate;
import com.study.manage.domain.enumm.BlogBehaviorType;
import com.study.manage.domain.enumm.BlogType;
import com.study.manage.utils.CmnArrayListUtils;
import com.study.manage.utils.DateTimeUtil;
import com.study.manage.utils.JsonMapper;

@Service
public class CommonService {

    private JsonMapper jsonMapper = new JsonMapper();

    @Autowired
    private EvaluateApplyWorkRepository evaluateApplyWorkRepository;
    @Autowired
    private EvaluateRepository evaluateRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private BlogBehaviorRepository blogBehaviorRepository;
    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private BlogMediaRepository blogMediaRepository;


    public Map<String, Date> getTimes(Date beginAt, Date endAt, List<Long> evaluateIds,
            Integer flg) {

        List<Object[]> objs = evaluateRepository.getBegEndTime(evaluateIds);
        Date createdAt = null;
        Date bsEndAt = null;
        for (Object[] obj : objs) {
            createdAt = (Timestamp) obj[0];
            bsEndAt = (Timestamp) obj[1];
        }
        if (flg != 0) {
            Date currentDate = new Date();
            if (bsEndAt.compareTo(currentDate) == 1) {
                endAt = currentDate;
            } else {
                endAt = bsEndAt;
            }
        }
        switch (flg) {
            case 7:
                beginAt = DateTimeUtil.addDays(endAt, -7);
                break;
            case 14:
                beginAt = DateTimeUtil.addDays(endAt, -14);
                break;
            case 30:
                beginAt = DateTimeUtil.addDays(endAt, -30);
                break;
            case 100:
                beginAt = createdAt;
                break;
            default:
                break;
        }
        Map<String, Date> timeMap = new HashMap<>();
        timeMap.put("beginAt", beginAt);
        timeMap.put("endAt", endAt);
        return timeMap;
    }

    public String[] getLables(Long id, int j) {
        Evaluate evaluate = evaluateRepository.findOne(id);
        List<Map<String, Object>> labelList = null;
        if (0 == j) {
            labelList = jsonMapper.fromJson(evaluate.parentInfoLabels, List.class);
        } else {
            labelList = jsonMapper.fromJson(evaluate.labels, List.class);
        }
        String[] lables = new String[0];
        if (labelList != null) {
            lables = new String[labelList.size()];
            for (int i = 0; i < labelList.size(); i++) {
                Map<String, Object> l = labelList.get(i);
                lables[i] = l.get("name").toString();
            }
        } else {
            if (1 == j && StringUtils.isNotEmpty(evaluate.labels)) {
                lables = evaluate.labels.split(",");
            }
            if (0 == j && StringUtils.isNotEmpty(evaluate.parentInfoLabels)) {
                lables = evaluate.parentInfoLabels.split(",");
            }
        }
        return lables;
    }

    public Map<String, Object> getCountNum(List<Long> es, Long referenceId) {

        Map<String, Object> map = new HashMap<>();
        map.put("applyCount", "0");
        map.put("applyCountDay", "0");
        map.put("blogCount", "0");
        map.put("blogCountDay", "0");
        map.put("orderCount", "0");
        map.put("orderCountDay", "0");
        map.put("openCount", "0");
        map.put("openCountDay", "0");
        map.put("showCount", "0");
        map.put("showCountDay", "0");
        map.put("hdCount", "0");
        map.put("hdCountDay", "0");
        if (!CmnArrayListUtils.isEmpty(es)) {
            Date date = new Date();
            int applyCount = evaluateApplyRepository.getEvaluateIdApplyCount(es, null, referenceId);
            int applyCountDay =
                    evaluateApplyRepository.getEvaluateIdApplyCount(es, date, referenceId);
            int blogCount = evaluateApplyWorkRepository.countByEvaluateIdInAndDay(es, null);
            int blogCountDay = evaluateApplyWorkRepository.countByEvaluateIdInAndDay(es, date);
            Float orderCount = orderRepository.sumPriceByEvaluateIdIn(es, null);
            Float orderCountDay = orderRepository.sumPriceByEvaluateIdIn(es, date);
            if (referenceId != null) {
                blogCount = evaluateApplyWorkRepository.countByReferenceId(es, null, referenceId);
                blogCountDay =
                        evaluateApplyWorkRepository.countByReferenceId(es, date, referenceId);
                orderCount = orderRepository.sumPriceByReferenceId(es, null, referenceId);
                orderCountDay = orderRepository.sumPriceByReferenceId(es, date, referenceId);
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            List<BlogBehavior> ljbehavior = blogBehaviorRepository
                    .findByCreatedAtAndType(cal.getTime(), BlogBehaviorType.LJ, es);
            List<BlogBehavior> xzbehavior = blogBehaviorRepository
                    .findByCreatedAtAndType(cal.getTime(), BlogBehaviorType.XZ, es);
            Integer ljOpened = 0;
            Integer xzOpened = 0;
            Integer ljshowed = 0;
            Integer xzshowed = 0;
            Integer ljhudong = 0;
            Integer xzhudong = 0;
            for (BlogBehavior ljbb : ljbehavior) {
                ljOpened += ljbb.opened;
                ljshowed += ljbb.showed;
                ljhudong += ljbb.hudong;
            }
            for (BlogBehavior xzbb : xzbehavior) {
                xzOpened += xzbb.opened;
                xzshowed += xzbb.showed;
                xzhudong += xzbb.hudong;
            }
            map.put("applyCount", applyCount);
            map.put("applyCountDay", applyCountDay);
            map.put("blogCount", blogCount);
            map.put("blogCountDay", blogCountDay);
            map.put("orderCount", orderCount == null ? 0 : orderCount);
            map.put("orderCountDay", orderCountDay == null ? 0 : orderCountDay);
            map.put("openCount", ljOpened);
            map.put("openCountDay", xzOpened);
            map.put("showCount", ljshowed);
            map.put("showCountDay", xzshowed);
            map.put("hdCount", ljhudong);
            map.put("hdCountDay", xzhudong);
        }
        return map;
    }

    public String getCoverImg(Long id) {
        String url = "";
        List<Blog> blogs = blogRepository.findByTypeAndExtId(BlogType.AdminEvaluateGroup, id);
        if (blogs != null && !blogs.isEmpty()) {
            url = blogMediaRepository.findOneImgUrlByBlogId(blogs.get(0).id);
        }
        return url;
    }

}
